library(survey)
library(foreign)
library(ggplot2)
library(cowplot)
library(dplyr)
library(egg)
library(ggpubr)
library(gridGraphics)

## see wave2_CLEANING.R for data cleaning
load("~/Dropbox/Public_Conf_Mil/Data_and_code/wave2_data/clean_dataw2.RData")

## Create weighted survey design object
w2_design <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = df
  )

addline_format <- function(x,...){
  gsub('\\s','\n',x)
}

#### TREATMENT EFFECTS FOR PUBLIC CONFIDENCE ####
  
treat_df <- data.frame(otva = c(svyttest(Q11_d ~ A1_6, w2_design)$estimate,
                                svyttest(Q11_d ~ A1_7, w2_design)$estimate,
                                svyttest(Q11_d ~ A1_8, w2_design)$estimate),
                       ci_lo = c(svyttest(Q11_d ~ A1_6, w2_design)$conf.int[1],
                                 svyttest(Q11_d ~ A1_7, w2_design)$conf.int[1],
                                 svyttest(Q11_d ~ A1_8, w2_design)$conf.int[1]),
                       ci_hi = c(svyttest(Q11_d ~ A1_6, w2_design)$conf.int[2],
                                 svyttest(Q11_d ~ A1_7, w2_design)$conf.int[2],
                                 svyttest(Q11_d ~ A1_8, w2_design)$conf.int[2]),
                       cond = c(1:3))

fig72_a <- ggplot(treat_df) + 
  geom_pointrange(aes(x = cond, y = otva, ymin = ci_lo, ymax = ci_hi), fatten = 2, size = 0.5) +
  geom_text(aes(x = cond, y = otva, label = sprintf("%0.2f", round(otva, digits = 2))), 
            size = 3,
            vjust = 2.5) + xlab("Treatment Condition") + 
  scale_x_continuous(breaks = c(1:3),
                     labels = addline_format(c("Republican Military", "Democrat Military",
                                               "Partisan Military"))) +
  ylab("Difference in Means from Control Condition") + ylim(-0.2, 0.2) +
  geom_hline(yintercept = 0, colour = "red", lty = 1) + theme_bw()
fig72_a
ggsave("~/Dropbox/Public_Conf_Mil/Chapter 6 Figures/wave2_diff_treateffects.jpeg", 
       plot = fig72_a, dpi = 1000)

#### TREATMENT EFFECTS BY PARTY ####

treat2_df <- data.frame(rep = c(svyttest(Q11_d ~ A1_6_rep, w2_design)$estimate,
                                svyttest(Q11_d ~ A1_7_rep, w2_design)$estimate,
                                svyttest(Q11_d ~ A1_8_rep, w2_design)$estimate),
                        dem = c(svyttest(Q11_d ~ A1_6_dem, w2_design)$estimate,
                                svyttest(Q11_d ~ A1_7_dem, w2_design)$estimate,
                                svyttest(Q11_d ~ A1_8_dem, w2_design)$estimate),
                        ind = c(svyttest(Q11_d ~ A1_6_ind, w2_design)$estimate,
                                svyttest(Q11_d ~ A1_7_ind, w2_design)$estimate,
                                svyttest(Q11_d ~ A1_8_ind, w2_design)$estimate),
                        rep_lo = c(svyttest(Q11_d ~ A1_6_rep, w2_design)$conf.int[1],
                                   svyttest(Q11_d ~ A1_7_rep, w2_design)$conf.int[1],
                                   svyttest(Q11_d ~ A1_8_rep, w2_design)$conf.int[1]),
                        rep_hi = c(svyttest(Q11_d ~ A1_6_rep, w2_design)$conf.int[2],
                                   svyttest(Q11_d ~ A1_7_rep, w2_design)$conf.int[2],
                                   svyttest(Q11_d ~ A1_8_rep, w2_design)$conf.int[2]),
                        dem_lo = c(svyttest(Q11_d ~ A1_6_dem, w2_design)$conf.int[1],
                                   svyttest(Q11_d ~ A1_7_dem, w2_design)$conf.int[1],
                                   svyttest(Q11_d ~ A1_8_dem, w2_design)$conf.int[1]),
                        dem_hi = c(svyttest(Q11_d ~ A1_6_dem, w2_design)$conf.int[2],
                                   svyttest(Q11_d ~ A1_7_dem, w2_design)$conf.int[2],
                                   svyttest(Q11_d ~ A1_8_dem, w2_design)$conf.int[2]),
                        ind_lo = c(svyttest(Q11_d ~ A1_6_ind, w2_design)$conf.int[1],
                                   svyttest(Q11_d ~ A1_7_ind, w2_design)$conf.int[1],
                                   svyttest(Q11_d ~ A1_8_ind, w2_design)$conf.int[1]),
                        ind_hi = c(svyttest(Q11_d ~ A1_6_ind, w2_design)$conf.int[2],
                                   svyttest(Q11_d ~ A1_7_ind, w2_design)$conf.int[2],
                                   svyttest(Q11_d ~ A1_8_ind, w2_design)$conf.int[2]),
                        dcond = c(1,5,9),
                        icond = c(2,6,10),
                        rcond = c(3,7,11))

fig72_b <- ggplot(treat2_df) + 
  geom_pointrange(aes(x = rcond, y = rep, ymin = rep_lo, ymax = rep_hi), color = "red") +
  geom_pointrange(aes(x = icond, y = ind, ymin = ind_lo, ymax = ind_hi), color = "gray60") +
  geom_pointrange(aes(x = dcond, y = dem, ymin = dem_lo, ymax = dem_hi), color = "blue") +
  geom_text(aes(x = rcond, y = rep, label = sprintf("%0.2f", round(rep, digits = 2))), size = 3,
            vjust = 2) +
  geom_text(aes(x = icond, y = ind, label = sprintf("%0.2f", round(ind, digits = 2))), size = 3,
            vjust = 2) +
  geom_text(aes(x = dcond, y = dem, label = sprintf("%0.2f", round(dem, digits = 2))), size = 3,
            vjust = 2) + xlab("Treatment Condition") + 
  scale_x_continuous(breaks = c(2, 6, 10),
                     labels = addline_format(c("Republican Military", "Democrat Military",
                                               "Partisan Military"))) +
  ylab("Difference in Means from Control Condition") +
  geom_hline(yintercept = 0, colour = "black", lty = 1) +
  theme_bw()
fig72_b
ggsave("~/Dropbox/Public_Conf_Mil/Chapter 6 Figures/wave2_diff_treatparty.jpeg", 
       plot = fig72_b, dpi = 1000)

#### TREATMENT EFFECTS - NEGATIVE PARTISANSHIP ####

copart <- svyby(~Q11_d, ~A1_copartisan, w2_design, svymean, na.rm = TRUE)

np_df <- data.frame(outs = c(NA, copart[,2], NA),
                    ci_lo = c(NA, copart[,2] - 1.96*copart[,3], NA),
                    ci_hi = c(NA, copart[,2] + 1.96*copart[,3], NA),
                    cond = c(1.5,2,3,4,4.5))

fig7_np <- ggplot(np_df) + 
  geom_pointrange(aes(x = cond, y = outs, ymin = ci_lo, ymax = ci_hi), fatten = 2, size = 0.5) +
  geom_text(aes(x = cond, y = outs, label = sprintf("%0.2f", round(outs, digits = 2))), 
            size = 3,
            vjust = 2.5) + xlab("") + 
  scale_x_continuous(breaks = c(2,3,4),
                     labels = addline_format(c("Control", "Co-Partisan", "Cross-Partisan"))) +
  ylab("Respondent Confidence in Military") + ylim(0.4,0.90) +
  ggtitle("Wave 2") +
  theme_bw()
fig7_np
ggsave("~/Dropbox/Public_Conf_Mil/Chapter 6 Figures/wave2_np.jpeg", 
       plot = fig7_np, dpi = 1000)

## Combine with wave 1 (code for fig7_np1 in Ch7_NORC_W1_diffinmeans.R)
fig7npb <- ggarrange(fig7_np1 + theme(axis.text.y = element_text(size = 6),
                                      plot.title = element_text(size = 11),
                                      axis.title.y = element_text(size = 8),
                                      axis.text.x = element_text(size = 7.5),
                                      axis.title.x = element_text(size = 8)),
                     fig7_np + theme(axis.text.y = element_text(size = 6),
                                     plot.title = element_text(size = 11),
                                     axis.title.y = element_text(size = 8),
                                     axis.text.x = element_text(size = 7.5),
                                     axis.title.x = element_text(size = 8)),
                     nrow = 1, ncol = 2)
fig7npb
ggsave("~/Dropbox/Public_Conf_Mil/Chapter 6 Figures/nonpartisan.jpeg", 
       plot = fig7npb, dpi = 700)


#### TREATMENT EFFECTS - NEGATIVE PARTISANSHIP, DIFFERENCE-IN-MEANS ####

copart_df <- data.frame(otva = c(NA, svyttest(Q11_d ~ A1_copartisanc, w2_design)$estimate,
                                 svyttest(Q11_d ~ A1_copartisanx, w2_design)$estimate, NA),
                        ci_lo = c(NA, svyttest(Q11_d ~ A1_copartisanc, w2_design)$conf.int[1],
                                  svyttest(Q11_d ~ A1_copartisanx, w2_design)$conf.int[1], NA),
                        ci_hi = c(NA, svyttest(Q11_d ~ A1_copartisanc, w2_design)$conf.int[2],
                                  svyttest(Q11_d ~ A1_copartisanx, w2_design)$conf.int[2], NA),
                        cond = c(0.5,1,2,2.5))

fignp_d <- ggplot(copart_df) + 
  geom_hline(yintercept = 0, colour = "red", lty = 1) + 
  geom_pointrange(aes(x = cond, y = otva, ymin = ci_lo, ymax = ci_hi), fatten = 2, size = 0.5) +
  geom_text(aes(x = cond, y = otva, label = sprintf("%0.2f", round(otva, digits = 2))), 
            size = 3,
            vjust = 2.5) + xlab("Treatment Condition") + 
  scale_x_continuous(breaks = c(1,2),
                     labels = c("Co-Partisan", "Cross-Partisan")) +
  ylab("Difference in Means from Control Condition") + ylim(-0.2, 0.2) +
  ggtitle("Wave 2") + theme_bw()
fignp_d
ggsave("~/Dropbox/Public_Conf_Mil/Chapter 6 Figures/wave2_np_diff.jpeg", 
       plot = fignp_d, dpi = 700)

## Combine with wave 1 (code for fig7_np1 in Ch7_NORC_W1_diffinmeans.R)
fig7npdif <- ggarrange(fignp_d1 + theme(axis.text.y = element_text(size = 6),
                                        plot.title = element_text(size = 11),
                                        axis.title.y = element_text(size = 8),
                                        axis.text.x = element_text(size = 7.5),
                                        axis.title.x = element_text(size = 8)),
                       fignp_d + theme(axis.text.y = element_text(size = 6),
                                       plot.title = element_text(size = 11),
                                       axis.title.y = element_text(size = 8),
                                       axis.text.x = element_text(size = 7.5),
                                       axis.title.x = element_text(size = 8)),
                       nrow = 1, ncol = 2)
fig7npdif
ggsave("~/Dropbox/Public_Conf_Mil/Chapter 6 Figures/nonpartisan_diff.jpeg", 
       plot = fig7npdif, dpi = 700)


#### MILITARY CUEING AND VOTE CHOICE ####

treat3_df <- data.frame(otva = c(svyttest(Q36_trump ~ A5_2, w2_design)$estimate,
                                 svyttest(Q36_trump ~ A5_3, w2_design)$estimate,
                                 svyttest(Q36_trump ~ A5_4, w2_design)$estimate,
                                 svyttest(Q36_trump ~ A5_5, w2_design)$estimate),
                        ci_lo = c(svyttest(Q36_trump ~ A5_2, w2_design)$conf.int[1],
                                  svyttest(Q36_trump ~ A5_3, w2_design)$conf.int[1],
                                  svyttest(Q36_trump ~ A5_4, w2_design)$conf.int[1],
                                  svyttest(Q36_trump ~ A5_5, w2_design)$conf.int[1]),
                        ci_hi = c(svyttest(Q36_trump ~ A5_2, w2_design)$conf.int[2],
                                  svyttest(Q36_trump ~ A5_3, w2_design)$conf.int[2],
                                  svyttest(Q36_trump ~ A5_4, w2_design)$conf.int[2],
                                  svyttest(Q36_trump ~ A5_5, w2_design)$conf.int[2]),
                        cond = c(1:4))

fig72_c <- ggplot(treat3_df) + 
  geom_pointrange(aes(x = cond, y = otva, ymin = ci_lo, ymax = ci_hi), fatten = 2, size = 0.5) +
  geom_text(aes(x = cond, y = otva, label = sprintf("%0.2f", round(otva, digits = 2))), 
            size = 3,
            vjust = 2.5) + xlab("Treatment Condition") + 
  scale_x_continuous(breaks = c(1:4),
                     labels = addline_format(c("Military: Biden", 
                                               "Military: Trump",
                                               "Generals: Biden",
                                               "Generals: Trump"))) +
  ylab("Difference in Means from Control Condition") + ylim(-0.2, 0.2) +
  geom_hline(yintercept = 0, colour = "red", lty = 1) + theme_bw()
fig72_c
ggsave("~/Dropbox/Public_Conf_Mil/Chapter 6 Figures/wave2_diff_trump.jpeg", 
       plot = fig72_c, dpi = 1000)

#### CUEING AND VOTE CHOICE BY PARTY ####

treat4_df <- data.frame(rep = c(svyttest(Q36_trump ~ A5_2_rep, w2_design)$estimate,
                                svyttest(Q36_trump ~ A5_3_rep, w2_design)$estimate,
                                svyttest(Q36_trump ~ A5_4_rep, w2_design)$estimate,
                                svyttest(Q36_trump ~ A5_5_rep, w2_design)$estimate),
                        dem = c(svyttest(Q36_trump ~ A5_2_dem, w2_design)$estimate,
                                svyttest(Q36_trump ~ A5_3_dem, w2_design)$estimate,
                                svyttest(Q36_trump ~ A5_4_dem, w2_design)$estimate,
                                svyttest(Q36_trump ~ A5_5_dem, w2_design)$estimate),
                        ind = c(svyttest(Q36_trump ~ A5_2_ind, w2_design)$estimate,
                                svyttest(Q36_trump ~ A5_3_ind, w2_design)$estimate,
                                svyttest(Q36_trump ~ A5_4_ind, w2_design)$estimate,
                                svyttest(Q36_trump ~ A5_5_ind, w2_design)$estimate),
                        rep_lo = c(svyttest(Q36_trump ~ A5_2_rep, w2_design)$conf.int[1],
                                   svyttest(Q36_trump ~ A5_3_rep, w2_design)$conf.int[1],
                                   svyttest(Q36_trump ~ A5_4_rep, w2_design)$conf.int[1],
                                   svyttest(Q36_trump ~ A5_5_rep, w2_design)$conf.int[1]),
                        rep_hi = c(svyttest(Q36_trump ~ A5_2_rep, w2_design)$conf.int[2],
                                   svyttest(Q36_trump ~ A5_3_rep, w2_design)$conf.int[2],
                                   svyttest(Q36_trump ~ A5_4_rep, w2_design)$conf.int[2],
                                   svyttest(Q36_trump ~ A5_5_rep, w2_design)$conf.int[2]),
                        dem_lo = c(svyttest(Q36_trump ~ A5_2_dem, w2_design)$conf.int[1],
                                   svyttest(Q36_trump ~ A5_3_dem, w2_design)$conf.int[1],
                                   svyttest(Q36_trump ~ A5_4_dem, w2_design)$conf.int[1],
                                   svyttest(Q36_trump ~ A5_5_dem, w2_design)$conf.int[1]),
                        dem_hi = c(svyttest(Q36_trump ~ A5_2_dem, w2_design)$conf.int[2],
                                   svyttest(Q36_trump ~ A5_3_dem, w2_design)$conf.int[2],
                                   svyttest(Q36_trump ~ A5_4_dem, w2_design)$conf.int[2],
                                   svyttest(Q36_trump ~ A5_5_dem, w2_design)$conf.int[2]),
                        ind_lo = c(svyttest(Q36_trump ~ A5_2_ind, w2_design)$conf.int[1],
                                   svyttest(Q36_trump ~ A5_3_ind, w2_design)$conf.int[1],
                                   svyttest(Q36_trump ~ A5_4_ind, w2_design)$conf.int[1],
                                   svyttest(Q36_trump ~ A5_5_ind, w2_design)$conf.int[1]),
                        ind_hi = c(svyttest(Q36_trump ~ A5_2_ind, w2_design)$conf.int[2],
                                   svyttest(Q36_trump ~ A5_3_ind, w2_design)$conf.int[2],
                                   svyttest(Q36_trump ~ A5_4_ind, w2_design)$conf.int[2],
                                   svyttest(Q36_trump ~ A5_5_ind, w2_design)$conf.int[2]),
                        dcond = c(1,5,9,13),
                        icond = c(2,6,10,14),
                        rcond = c(3,7,11,15))

fig72_d <- ggplot(treat4_df) + 
  geom_pointrange(aes(x = rcond, y = rep, ymin = rep_lo, ymax = rep_hi), color = "red") +
  geom_pointrange(aes(x = icond, y = ind, ymin = ind_lo, ymax = ind_hi), color = "gray60") +
  geom_pointrange(aes(x = dcond, y = dem, ymin = dem_lo, ymax = dem_hi), color = "blue") +
  geom_text(aes(x = rcond, y = rep, label = sprintf("%0.2f", round(rep, digits = 2))), size = 3,
            vjust = 2) +
  geom_text(aes(x = icond, y = ind, label = sprintf("%0.2f", round(ind, digits = 2))), size = 3,
            vjust = 2) +
  geom_text(aes(x = dcond, y = dem, label = sprintf("%0.2f", round(dem, digits = 2))), size = 3,
            vjust = 2) + xlab("Treatment Condition") + 
  scale_x_continuous(breaks = c(2, 6, 10, 14),
                     labels = addline_format(c("Military: Biden", 
                                               "Military: Trump",
                                               "Generals: Biden",
                                               "Generals: Trump"))) +
  ylab("Difference in Means from Control Condition") +
  geom_hline(yintercept = 0, colour = "black", lty = 1) +
  theme_bw()
fig72_d
ggsave("~/Dropbox/Public_Conf_Mil/Chapter 6 Figures/wave2_diff_trumpbyparty.jpeg", 
       plot = fig72_d, dpi = 1000)

#### TREATMENT EFFECTS FOR INSURRECTION ACT BY PARTY ####

treat5_df <- data.frame(rep = c(svyttest(Q25A_d ~ A3A_2_rep, w2_design)$estimate,
                                svyttest(Q25A_d ~ A3A_3_rep, w2_design)$estimate),
                        dem = c(svyttest(Q25A_d ~ A3A_2_dem, w2_design)$estimate,
                                svyttest(Q25A_d ~ A3A_3_dem, w2_design)$estimate),
                        rep_lo = c(svyttest(Q25A_d ~ A3A_2_rep, w2_design)$conf.int[1],
                                   svyttest(Q25A_d ~ A3A_3_rep, w2_design)$conf.int[1]),
                        rep_hi = c(svyttest(Q25A_d ~ A3A_2_rep, w2_design)$conf.int[2],
                                   svyttest(Q25A_d ~ A3A_3_rep, w2_design)$conf.int[2]),
                        dem_lo = c(svyttest(Q25A_d ~ A3A_2_dem, w2_design)$conf.int[1],
                                   svyttest(Q25A_d ~ A3A_3_dem, w2_design)$conf.int[1]),
                        dem_hi = c(svyttest(Q25A_d ~ A3A_2_dem, w2_design)$conf.int[2],
                                   svyttest(Q25A_d ~ A3A_3_dem, w2_design)$conf.int[2]),
                        dcond = c(1,4),
                        rcond = c(2,5))

fig72_e <- ggplot(treat5_df) +
  geom_pointrange(aes(x = rcond, y = rep, ymin = rep_lo, ymax = rep_hi), color = "red") +
  geom_pointrange(aes(x = dcond, y = dem, ymin = dem_lo, ymax = dem_hi), color = "blue") +
  geom_text(aes(x = rcond, y = rep, label = sprintf("%0.2f", round(rep, digits = 2))), 
            size = 3, vjust = 2) +
  geom_text(aes(x = dcond, y = dem, label = sprintf("%0.2f", round(dem, digits = 2))), 
            size = 3, vjust = 2) +
  xlab("Treatment Condition") + ylab("Difference in Means from Control Condition") +
  geom_hline(yintercept = 0, colour = "black", lty = 1) + ylim(-0.2,0.2) +
  scale_x_continuous(breaks = c(1.5,4.5),
                     labels = addline_format(c("Oppose",
                                               "Support"))) +
  theme_bw()
fig72_e
ggsave("~/Dropbox/Public_Conf_Mil/Chapter 6 Figures/wave2_diff_insurrectionact.jpeg", 
       plot = fig72_e, dpi = 1000)

#### TREATMENT EFFECTS - WAVE 1 AND WAVE 2 ####

## wave 1 set up in Ch7_NORC_W1_diffinmeans.R

### Side-by-side

fig71_a1 <- ggplot(treax_df) + ## wave 1
  geom_hline(yintercept = 0, colour = "red", lty = 1) + 
  geom_pointrange(aes(x = cond, y = otva, ymin = ci_lo, ymax = ci_hi), fatten = 2, size = 0.5) +
  geom_text(aes(x = cond, y = otva, label = sprintf("%0.2f", round(otva, digits = 2))), 
            size = 3,
            vjust = 2.5) + xlab("Treatment Condition") + 
  scale_x_continuous(breaks = c(1:3),
                     labels = addline_format(c("Republican Military", "Democrat Military",
                                               "Polarized Military"))) +
  ggtitle("Wave 1") +
  ylab("Difference in Means from Control Condition") + ylim(-0.2, 0.2) +
  theme_bw()

fig72_a1 <- ggplot(treat_df) + 
  geom_hline(yintercept = 0, colour = "red", lty = 1) + 
  geom_pointrange(aes(x = cond, y = otva, ymin = ci_lo, ymax = ci_hi), fatten = 2, size = 0.5) +
  geom_text(aes(x = cond, y = otva, label = sprintf("%0.2f", round(otva, digits = 2))), 
            size = 3,
            vjust = 2.5) + xlab("Treatment Condition") + 
  scale_x_continuous(breaks = c(1:3),
                     labels = addline_format(c("Republican Military", "Democrat Military",
                                               "Partisan Military"))) +
  ggtitle("Wave 2") +
  ylab("Difference in Means from Control Condition") + ylim(-0.2, 0.2) +
  theme_bw()

fig7_1a <- ggarrange(fig71_a1 + theme(axis.text.y = element_text(size = 6),
                                      plot.title = element_text(size = 11),
                                      axis.title.y = element_text(size = 8),
                                      axis.text.x = element_text(size = 7.5),
                                      axis.title.x = element_text(size = 8)),
                     fig72_a1 + theme(axis.text.y = element_text(size = 6),
                                      plot.title = element_text(size = 11),
                                      axis.title.y = element_text(size = 8),
                                      axis.text.x = element_text(size = 7.5),
                                      axis.title.x = element_text(size = 8)),
                     nrow = 1, ncol = 2)
fig7_1a
ggsave("~/Dropbox/Public_Conf_Mil/Chapter 6 Figures/wave1and2_effects1.jpeg", 
       plot = fig7_1a, dpi = 1000)

### One plot

waves_df <- data.frame(waves = c(treax_df$otva, NA, treat_df$otva),
                       ci_lo = c(treax_df$ci_lo, NA, treat_df$ci_lo),
                       ci_hi = c(treax_df$ci_hi, NA, treat_df$ci_hi),
                       cond = c(1:7))

fig7_1b <- ggplot(waves_df) + 
  geom_hline(yintercept = 0, colour = "red", lty = 1) + 
  geom_pointrange(aes(x = cond, y = waves, ymin = ci_lo, ymax = ci_hi), fatten = 2, size = 0.5) +
  geom_text(aes(x = cond, y = waves, label = sprintf("%0.2f", round(waves, digits = 2))), 
            size = 3,
            vjust = 2.5) + 
  xlab("Wave 1 Condition                                                              Wave 2 Condition") + 
  scale_x_continuous(breaks = c(1,2,3,5,6,7),
                     labels = addline_format(c("Republican Military", "Democrat Military",
                                               "Polarized Military",
                                               "Republican Military", "Democrat Military",
                                               "Partisan Military"))) +
  ylab("Difference in Means from Control Condition") + ylim(-0.2, 0.2) +
  theme_bw()
fig7_1b
ggsave("~/Dropbox/Public_Conf_Mil/Chapter 6 Figures/wave1and2_effects2.jpeg", 
       plot = fig7_1b, dpi = 1000)

#### TREATMENT EFFECTS - WAVE 1 AND WAVE 2 BY PARTY ####

## wave 1 set up in Ch7_NORC_W1_diffinmeans.R

### Side-by-side

fig71_b1 <- ggplot(treax2_df) + 
  geom_hline(yintercept = 0, colour = "black", lty = 1) +
  geom_pointrange(aes(x = rcond, y = rep, ymin = rep_lo, ymax = rep_hi), color = "red") +
  geom_pointrange(aes(x = icond, y = ind, ymin = ind_lo, ymax = ind_hi), color = "gray60") +
  geom_pointrange(aes(x = dcond, y = dem, ymin = dem_lo, ymax = dem_hi), color = "blue") +
  geom_text(aes(x = rcond, y = rep, label = sprintf("%0.2f", round(rep, digits = 2))), size = 3,
            vjust = 2) +
  geom_text(aes(x = icond, y = ind, label = sprintf("%0.2f", round(ind, digits = 2))), size = 3,
            vjust = 2) +
  geom_text(aes(x = dcond, y = dem, label = sprintf("%0.2f", round(dem, digits = 2))), size = 3,
            vjust = 2) + xlab("Treatment Condition") + 
  scale_x_continuous(breaks = c(2, 6, 10),
                     labels = addline_format(c("Republican Military", "Democrat Military",
                                               "Polarized Military"))) +
  ggtitle("Wave 1") + ylim(-0.35,0.35) +
  ylab("Difference in Means from Control Condition") +
  theme_bw()

fig72_b1 <- ggplot(treat2_df) + 
  geom_hline(yintercept = 0, colour = "black", lty = 1) +
  geom_pointrange(aes(x = rcond, y = rep, ymin = rep_lo, ymax = rep_hi), color = "red") +
  geom_pointrange(aes(x = icond, y = ind, ymin = ind_lo, ymax = ind_hi), color = "gray60") +
  geom_pointrange(aes(x = dcond, y = dem, ymin = dem_lo, ymax = dem_hi), color = "blue") +
  geom_text(aes(x = rcond, y = rep, label = sprintf("%0.2f", round(rep, digits = 2))), size = 3,
            vjust = 2) +
  geom_text(aes(x = icond, y = ind, label = sprintf("%0.2f", round(ind, digits = 2))), size = 3,
            vjust = 2) +
  geom_text(aes(x = dcond, y = dem, label = sprintf("%0.2f", round(dem, digits = 2))), size = 3,
            vjust = 2) + xlab("Treatment Condition") + 
  scale_x_continuous(breaks = c(2, 6, 10),
                     labels = addline_format(c("Republican Military", "Democrat Military",
                                               "Partisan Military"))) +
  ylab("Difference in Means from Control Condition") +
  ggtitle("Wave 2") + ylim(-0.35,0.35) +
  theme_bw()

fig7_2a <- ggarrange(fig71_b1 + theme(axis.text.y = element_text(size = 6),
                                      plot.title = element_text(size = 11),
                                      axis.title.y = element_text(size = 8),
                                      axis.text.x = element_text(size = 7.5),
                                      axis.title.x = element_text(size = 8)),
                     fig72_b1 + theme(axis.text.y = element_text(size = 6),
                                      plot.title = element_text(size = 11),
                                      axis.title.y = element_text(size = 8),
                                      axis.text.x = element_text(size = 7.5),
                                      axis.title.x = element_text(size = 8)),
                     nrow = 1, ncol = 2)
fig7_2a
ggsave("~/Dropbox/Public_Conf_Mil/Chapter 6 Figures/wave1and2_effectsparty1.jpeg", 
       plot = fig7_2a, dpi = 1000)

### One plot

waves_df2 <- data.frame(rep = c(treax2_df$rep, treat2_df$rep),
                        dem = c(treax2_df$dem, treat2_df$dem),
                        ind = c(treax2_df$ind, treat2_df$ind),
                        rep_lo = c(treax2_df$rep_lo, treat2_df$rep_lo),
                        rep_hi = c(treax2_df$rep_hi, treat2_df$rep_hi),
                        dem_lo = c(treax2_df$dem_lo, treat2_df$dem_lo),
                        dem_hi = c(treax2_df$dem_hi, treat2_df$dem_hi),
                        ind_lo = c(treax2_df$ind_lo, treat2_df$ind_lo),
                        ind_hi = c(treax2_df$ind_hi, treat2_df$ind_hi),
                        dcond = c(1,5,9,15,19,23),
                        icond = c(2,6,10,16,20,24),
                        rcond = c(3,7,11,17,21,25))

fig7_2b <- ggplot(waves_df2) + 
  geom_hline(yintercept = 0, colour = "red", lty = 1) + 
  geom_pointrange(aes(x = rcond, y = rep, ymin = rep_lo, ymax = rep_hi), color = "red") +
  geom_pointrange(aes(x = icond, y = ind, ymin = ind_lo, ymax = ind_hi), color = "gray60") +
  geom_pointrange(aes(x = dcond, y = dem, ymin = dem_lo, ymax = dem_hi), color = "blue") +
  geom_text(aes(x = rcond, y = rep, label = sprintf("%0.2f", round(rep, digits = 2))), size = 3,
            vjust = 2) +
  geom_text(aes(x = icond, y = ind, label = sprintf("%0.2f", round(ind, digits = 2))), size = 3,
            vjust = 2) +
  geom_text(aes(x = dcond, y = dem, label = sprintf("%0.2f", round(dem, digits = 2))), size = 3,
            vjust = 2) + xlab("Treatment Condition") + 
  xlab("Wave 1 Condition                                                      Wave 2 Condition") + 
  scale_x_continuous(breaks = c(2,6,10,16,20,24),
                     labels = addline_format(c("Republican Military", "Democrat Military",
                                               "Polarized Military",
                                               "Republican Military", "Democrat Military",
                                               "Partisan Military"))) +
  ylab("Difference in Means from Control Condition") + ylim(-0.35, 0.35) +
  theme_bw()
fig7_2b
ggsave("~/Dropbox/Public_Conf_Mil/Chapter 6 Figures/wave1and2_effectsparty2.jpeg", 
       plot = fig7_2b, dpi = 1000)
